package com.dex4d.ui.wallet.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.dex4d.R;
import com.dex4d.async.KeyAsyncTask;
import com.dex4d.core.AppToolBarBaseActivity;
import com.dex4d.event.ExitEvent;
import com.dex4d.utils.BaseUtils;
import com.dex4d.utils.Commons;
import com.dex4d.utils.EthWalletUtil;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by chengyuchun on 2018/10/20.
 */
public class GenerateMnemonicActivity extends AppToolBarBaseActivity implements Commons.BUNDLE_KEYS {
    @BindView(R.id.recyclerView_mnemonic)
    RecyclerView mRecyclerViewMnemonic;

    protected CommonAdapter mCommonAdapter;
    private ArrayList<String> mMnemonicList = new ArrayList<String>();

    public static void start(Activity activity) {
        Intent intent = new Intent(activity, GenerateMnemonicActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected void initToolbar() {
        showBackView();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_generate_mnemonic;
    }

    @Override
    protected void initVariables() {
        EventBus.getDefault().register(this);
    }

    @Override
    protected void loadData() {
        mCommonAdapter = new CommonAdapter<String>(this, R.layout.item_generate_mnemonic, mMnemonicList) {
            @Override
            protected void convert(ViewHolder holder, final String word, int position) {
                holder.setText(R.id.tv_word, word);
            }
        };
        mRecyclerViewMnemonic.setLayoutManager(new GridLayoutManager(this, 3));
        mRecyclerViewMnemonic.setAdapter(mCommonAdapter);

        getMnemonic(); //生成助记词
    }

    @OnClick({R.id.btn_continue,R.id.tv_remind_later})
    public void onViewClick(View view) {
        switch (view.getId()){
            case R.id.btn_continue:
                Bundle bundle = new Bundle();
                bundle.putStringArrayList(MNEMONIC_CODE,mMnemonicList);
                VerifyMnemonicActivity.start(this,bundle);
                break;
            case R.id.tv_remind_later:
                showLoadingDialog();
                String randomPwd = BaseUtils.getRandomPwd(); //6位随机密码
                new KeyAsyncTask(randomPwd,mMnemonicList,false,this).execute(); //创建钱包
                break;
        }

    }

    /**
     * 获取
     */
    protected void getMnemonic() {
        getLoadingManager().showLoadingDialog();
        //获取助记词语
        new Thread(new Runnable() {
            @Override
            public void run() {
                mMnemonicList.clear();
                mMnemonicList.addAll(EthWalletUtil.getMnemonicCode());
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mCommonAdapter.notifyDataSetChanged();
                        dismissLoadingDialog();
                    }
                });
            }
        }).start();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(ExitEvent event) {
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}